﻿var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var scriptbase = hostweburl + "/_layouts/15/";


/*
// CSOM CODE - not implemented yet.

$.getScript(scriptbase + 'SP.Runtime.js', function () {
    $.getScript(scriptbase + 'SP.js', function () {
        $.getScript(scriptbase + 'SP.RequestExecutor.js', function () {
            var context = SP.ClientContext.get_current();
            web = context.get_web();
            user = web.get_currentUser();
            context.load(user);
            context.executeQueryAsync(function () {
                $('#message').text('Hello ' + user.get_title());
            }, function (sender, args) {
                alert('Failed to get user name. Error:' + args.get_message());
            });
        })
    })
});
*/

// REST CODE

var executor = Winsmarts.SP.RestProxy;
var url = this.appweburl + "/_api/SP.AppContextSite(@target)/web?" + "@target='" + this.hostweburl + "'";

executor.executeAsync({
    url: url,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        alert(data.body);
        // var jsonObject = JSON.parse(data.body);
    },
    error: function (data, errorCode, errorMessage) {
        alert(errorMessage);
    }
});

function getQueryStringParameter(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);
    var toReturn = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    if (toReturn == null) toReturn = "";
    return toReturn;
};